Skip to content

Conversation

@sandeepsalwan1
Copy link

@sandeepsalwan1 sandeepsalwan1 commented May 6, 2025

Get Top 10 HackerNews Articles daily

final

image
demoHackerNews.mov

Full script:

import asyncio
import datetime
from pathlib import Path
from typing import List

from pydantic import BaseModel
from langchain_openai import ChatOpenAI
from browser_use import Agent, Controller

# Define the output format as Pydantic models
class PostSummary(BaseModel):
    title: str
    url: str
    summary_points: List[str]

class DailySummary(BaseModel):
    date: str
    posts: List[PostSummary]

controller = Controller(output_model=DailySummary)

async def get_hn_summaries():
    """Get and summarize top 10 Show HN posts"""
    llm = ChatOpenAI(model='gpt-4o')
    
    # Define the task for the agent
    task = """
    Go to Hacker News Show HN section (https://news.ycombinator.com/show).
    
    Collect the top 10 Show HN posts. For each post:
    1. Get the title and URL
    2. Visit the URL
    3. Create exactly 3 bullet points that summarize the key points of the page
    
    Format the output according to the Pydantic model structure.
    """
    
    # Create and run the agent
    agent = Agent(
        task=task,
        llm=llm,
        controller=controller
    )
    
    print("Starting HN summary generation...\n")
    print("This will take a few minutes as the agent visits multiple pages.\n")
    
    try:
        history = await agent.run()
        
        result = history.final_result()
        if result:
            today = datetime.datetime.now().strftime('%Y-%m-%d')
            summary_data = DailySummary.model_validate_json(result)
            
            # Set today's date in the model
            summary_data.date = today
            
            save_to_markdown(summary_data)
            print(f"Successfully saved summaries to markdown!\n")
            return True
        else:
            print("No results were obtained\n")
            return False
    except Exception as e:
        print(f"Error running agent: {e}\n")
        return False

def save_to_markdown(summary: DailySummary):
    """Save the summary to a markdown file"""
    file_path = Path(f"hn_summary_{summary.date}.md")
    
    #Create markdown content
    content = f"# Hacker News Show HN Summary - {summary.date}\n\n"
    
    for i, post in enumerate(summary.posts, 1):
        content += f"## {i}. {post.title}\n"
        content += f"[Link]({post.url})\n\n"
        content += "### Summary\n"
        for point in post.summary_points:
            content += f"- {point}\n"
        content += "\n---\n\n"
    
    # Write to file
    file_path.write_text(content)
    print(f"Saved to {file_path}")

async def main():
    """Main function to run the HN summary generator"""
    print("\nHacker News TL;DR Summary Generator\n")
    print("This script will fetch the top 10 Show HN posts and summarize them in 3 bullet points each.\n")
    
    try:
        await get_hn_summaries()
        print("\nProcess completed successfully!")
    except KeyboardInterrupt:
        print("\nProcess interrupted by user. Exiting gracefully...")
    except Exception as e:
        print(f"\nAn unexpected error occurred: {e}")
    
    print("\nDone!")

if __name__ == "__main__":
    asyncio.run(main()) 

2/30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant